#include <iostream>
using namespace std;
#include <string>
#include <time.h>
#include <windows.h>
#include "registrymod.h"
#include "mathlogic.h"

#define TICK_MILLISECONDS 1000
#define DLL_EXPORT

//#define SOOMI
//#define SVERIGE
//#define DOITSE
#define QUE

MathLogic::MathLogic(string name, int level)
{
	player = name;
	if(player == "-h")
	{
		printHelp();
		exit(1);
	}
	
	pRegmod = new RegistryMod();
	pRegmod->readValueFromRegistry();
	hiscore = pRegmod->getHiscore();
	topPlayer = pRegmod->getPlayerName();
	
	difficulty = level;
	if(difficulty == 0)
		score = 500;
	else
		score = difficulty * 1000;
}

MathLogic::~MathLogic()
{
	delete pRegmod;
}

void MathLogic::RandomizeNumbers()
{
	srand ( time(NULL) );
 
	a = rand() % 10 + 1;
	b = rand() % 10 + 1;
	c = rand() % 10 + 1;
	d = rand() % 10 + 1;
}

void MathLogic::GenerateProblem()
{
	Start = clock() * CLK_TCK;
	switch(difficulty)
	{
	case 0:
		correctAnswer = a*b;
#ifdef SOOMI
			cout << "Paljonko on " << a << " * " << b << "?" << endl;
#endif
#ifdef SVERIGE
			cout << "Huur mykket eer " << a << " * " << b << "?" << endl;
#endif
#ifdef DOITSE
			cout << "WIE IST " << a << " * " << b << "??!" << endl;
#endif
#ifdef QUE
			cout << "Que? " << a << " * " << b << "?" << endl;
#endif
		cin >> answer;
		break;
	case 1:
		correctAnswer = a*b*c;
#ifdef SOOMI
			cout << "Paljonko on " << a << " * " << b << " * " << c << "?" << endl;
#endif
#ifdef SVERIGE
			cout << "Huur mykket eer " << a << " * " << b << " * " << c << "?" << endl;
#endif
#ifdef DOITSE
			cout << "WIE IST " << a << " * " << b << " * " << c << "?" << endl;
#endif
#ifdef QUE
			cout << "Que? " << a << " * " << b << " * " << c << "?" << endl;
#endif
		cin >> answer;
		break;

	case 2:
		correctAnswer = a*b*c*d;
#ifdef SOOMI
			cout << "Paljonko on " << a << " * " << b << " * " << c << " * " << d << "?" << endl;
#endif
#ifdef SVERIGE
			cout << "Huur mykket eer " << a << " * " << b << " * " << c << " * " << d << "?" << endl;
#endif
#ifdef DOITSE
			cout << "WIE IST " << a << " * " << b << " * " << c << " * " << d << "?" << endl;
#endif
#ifdef QUE
			cout << "Que? " << a << " * " << b << " * " << c << " * " << d << "?" << endl;
#endif
		cin >> answer;
		break;

	default:
		printHelp();
		break;
	}
	Stop = clock() * CLK_TCK;
	timeMs = (Stop - Start) / TICK_MILLISECONDS;
	checkAnswer();

}

void MathLogic::printHelp(void)
{
	cout << "Parameters:\nPlayer Difficulty[0-2]" << endl;
	cout << "0 - Easy\n1 - Normal\n2 - Haaaard" << endl;
	cout << "Usage: mathinator.exe player 2" << endl;
}

void MathLogic::checkAnswer(void)
{
	if(answer == correctAnswer)
	{
		system("cls");
		score *= 1.10;
		score -= timeMs / 100;
#ifdef SOOMI
		cout << "Oikein! " << " Pisteet: " << score << endl;
		cout << "Aika: " << timeMs << " ms" << endl << endl;
#endif
#ifdef SVERIGE
		cout << "Riktiik! " << " Poeeng: " << score << endl;
		cout << "Tiid: " << timeMs << " ms" << endl << endl;
#endif
#ifdef DOITSE
		cout << "DAS IST GUT! " << " DEINE SCHEIBEN PONGEN: " << score << endl;
		cout << "DEINE TIMME: " << timeMs << " MS" << endl << endl;
#endif
#ifdef QUE
		cout << "Si! " << "La puntuacion : " << score << endl;
		cout << "El Tiempo: " << timeMs << " ms" << endl << endl;
#endif
		RandomizeNumbers();
		GenerateProblem();
	}
	else
	{
#ifdef SOOMI
		cout << "\nHavisit pelin!" << endl << "\nPelaaja: " << player << "\nTaso: " << difficulty;
#endif
#ifdef SVERIGE
		cout << "\nDu loooos!" << endl << "\nSpeelaren: " << player << "\nNivoo: " << difficulty;
#endif
#ifdef DOITSE
		cout << "\nDU HAST LEVEL NICHT GEVERLOREN!" << endl << "\nSPELERINNE: " << player << "\nEBENE: " << difficulty;
#endif
#ifdef QUE
		cout << "\nHa perdido el juego!" << endl << "\nJugador: " << player << "\nNivel: " << difficulty;
#endif
		if(score > hiscore)
		{
#ifdef SOOMI
			cout << "\nUUSI HISCORE, LUU KURKKUUN!" << endl;
#endif
#ifdef SVERIGE
			cout << "\nNYA BEST POEENG!" << endl;
#endif
#ifdef DOITSE
			cout << "\nDAS BESTEN PONGEN!" << endl;
#endif
#ifdef QUE
			cout << "\nSI BUENOS RESULTADO GRANDES!" << endl;
#endif
			pRegmod->writeToRegistry(player, score);
		}

		pRegmod->readValueFromRegistry();
#ifdef SOOMI
		cout << "\nPisteet: " << score << endl;
		cout << "Kaikkien aikojen paras pelaaja: " << pRegmod->getPlayerName() << " - " << pRegmod->getHiscore() << endl;
#endif
#ifdef SVERIGE
		cout << "\nPoeengerna: " << score << endl;
		cout << "Tiderna basta spelare: " << pRegmod->getPlayerName() << " - " << pRegmod->getHiscore() << endl;
#endif
#ifdef DOITSE
		cout << "\nDAS PONGEN: " << score << endl;
		cout << "MEIN FUHRER!: " << pRegmod->getPlayerName() << " - " << pRegmod->getHiscore() << endl;
#endif
#ifdef QUE
		cout << "\nLa puntuacion: " << score << endl;
		cout << "Los Bastardo!: " << pRegmod->getPlayerName() << " - " << pRegmod->getHiscore() << endl;
#endif

		
		
	}
}